#pragma once
#include "../CommonModule/comm.hpp"
#include <iostream>
#include <stdexcept>
#include <string>

class Secure
{
public:

    // 报文的加密
    static bool encryption(const std::string &plaintext, std::string *ciphertext)
    {
        return true;
    }
    
    // 报文的解密
    static bool decrypt(const std::string &ciphertext, std::string *plaintext)
    {
        return true;
    }

    const static std::string BASE62_CHARS;

    // 整数转62进制字符串
    static std::string intToBase62(uint64_t num)
    {
        if (num == 0)
            return "00000"; // 确保结果总是5位

        std::string result;
        while (num > 0)
        {
            result = BASE62_CHARS[num % 62] + result;
            num /= 62;
        }

        // 不足5位时补前导零
        while (result.length() < 5)
        {
            result = '0' + result;
        }

        return result;
    }

    // 62进制字符串转整数
    static uint64_t base62ToInt(const std::string &str)
    {
        if (str.length() != 5)
        {
            throw std::invalid_argument("输入字符串长度必须为5");
        }

        uint64_t result = 0;
        for (char c : str)
        {
            size_t pos = BASE62_CHARS.find(c);
            if (pos == std::string::npos)
            {
                DLOG("非法字符: %s", std::string(1, c).c_str());
                throw std::invalid_argument("非法字符: " + std::string(1, c));
            }
            result = result * 62 + pos;
        }
        return result;
    }
};

const std::string Secure::BASE62_CHARS =  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";